package com.jwdf.uda.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentChangeNicknameBinding;
import com.jwdf.uda.event.IndividualEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.UserUpdateInfoAction;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.EventBusUtil;

import retrofit2.Response;

/**
 * Created by Administrator on 2016/6/22.
 */
public class ChangeNickNameActivity extends AbsTitleBarActivity {

    public static void start(Context context, String original) {
        Intent intent = new Intent(context, ChangeNickNameActivity.class);
        intent.putExtra("nick", original);
        context.startActivity(intent);
    }

    @Override
    protected String getTopTitle() {
        return "修改昵称";
    }

    @Override
    protected BaseFragment onCreateContentFragment() {
        return new NickNameFragment();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBusUtil.registerEvent(this);
    }
    @Override
    protected Bundle onPutArguments() {
        Bundle args = new Bundle();
        args.putString("nick", getIntent().getStringExtra("nick"));
        return args;
    }
    @Override
    protected void onDestroy() {
        EventBusUtil.unregisterEvent(this);
        super.onDestroy();
    }

    public void onEvent(IndividualEvent event) {
        finish();
    }

    public static class NickNameFragment extends BaseFragment {
        private FragmentChangeNicknameBinding mDataBinding;
        private String mOriginalNick;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            mDataBinding = FragmentChangeNicknameBinding.inflate(inflater, container, false);
            initViews();
            return mDataBinding.getRoot();
        }

        private void initViews() {
            mOriginalNick = getArguments().getString("nick", "");
            mDataBinding.etNick.setText(mOriginalNick);
            mDataBinding.etNick.setSelection(mOriginalNick.length());
            mDataBinding.btnSetChange.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String nickName = mDataBinding.etNick.getText().toString().trim();
                    if(TextUtils.equals(nickName,mOriginalNick)){
                        getActivity().finish();
                    }else {
                        if (!TextUtils.isEmpty(nickName)) {
                            updateUserInfo(nickName);
                        }
                    }
                }
            });
        }

        private void updateUserInfo(String nickName) {
            showProgressDialog();
            onNewRequestCall(UserUpdateInfoAction.newInstance(getContext(), nickName, "", "").request(new AHttpService.IResCallback<PbBean.PbResUserUpdateInfo>() {
                @Override
                public void onCallback(int resultCode, Response<PbBean.PbResUserUpdateInfo> response, PbBean.PbBaseServerInfo baseServerInfo) {
                    if (getView() != null) {
                        closeProgressDialog();
                        if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                            return;
                        }
                        PbBean.PbResUserUpdateInfo info = response.body();
                        IndividualEvent event = new IndividualEvent(IndividualEvent.USER_NICKNAME_EVENT);
                        event.userInfo = info.getUserInfo();
                        EventBusUtil.post(event);
                    }
                }
            }));
        }

    }
}
